const DoctorData = require('../model/DoctorData');

// 会连表查询的表

// 获取所有医生信息
exports.getdoctorlist = async (req, res) => {
    // limit返回数,offset偏移量
    let { limit, offset } = req.query;
    let limitnum = limit ? limit : 10;
    let offsetnum = offset ? offset : 0;

    const doctorlist = await DoctorData.find()
        .sort({ time: -1 })
        .skip(offsetnum)
        .limit(limitnum);
    const doctornum = await DoctorData.find().countDocuments();
    res.json({
        doctornum,
        code: 0,
        data: doctorlist,
        doctornum,
    });
};

// 筛选数据getscreen
exports.getscreen = async (req, res) => {
    // Doctorname医生姓名,Department科室
    let { Department, Doctorname, limit, offset } = req.body;

    // Department = Department ? Department : ''
    // Doctorname = Doctorname ? Doctorname : ''

    let limitnum = limit ? limit : 10;
    let offsetnum = offset ? offset : 0;

    console.log('Doctorname', Doctorname);

    const data = await DoctorData.find({
        Department: Department,
        $or: [{ Doctorname: { $regex: Doctorname } }],
    })
        .skip(offsetnum)
        .limit(limitnum);

    console.log('data.length', data.length);

    res.json({
        code: 0,
        data: data,
        datanum: data.length,
    });
};

// 根据id数组删除
exports.delarr = async (req, res) => {
    let { id } = req.body;
    console.log('id', id.length);

    await Promise.all(
        id.map((item) => {
            console.log(item);
            return DoctorData.findByIdAndDelete({ _id: item });
        })
    );

    res.json({
        code: 0,
        message: '删除成功',
    });
};

// 根据id编辑详情
exports.edit = async (req, res) => {
    let { id } = req.query;

    const data = await DoctorData.findById({ _id: id });

    if (Object.keys(data).length != 0) {
        res.json({
            code: 0,
            data,
        });
    } else {
        res.json({
            code: 500,
            message: '请求失败',
        });
    }
};

// 新增
exports.add = async (req, res) => {
    let {
        name,
        adress,
        department,
        doctor,
        registrationfee,
        examinationfee,
        introduction,
        status,
        tags,
    } = req.body;
    console.log('req.body', req.body);

    introduction = introduction == 'undefined' ? '是个医生' : introduction;

    department = department.replace(/[',]/g, '-');
    console.log('department', department);

    let avatarurl = 'http://localhost:8088/' + req.file.filename;

    console.log('avatarurl', avatarurl);
    tags = tags.replace(/"/g, '').split(',');

    const data = await DoctorData.create({
        Doctorname: name,
        avatar: avatarurl,
        Doctorjob: doctor,
        Department: department,
        Tags: tags,
        Doctorcontent: introduction,
        Courtyardarea: adress,
        time: new Date(),
    });

    if (Object.keys(data).length != 0) {
        res.json({
            code: 0,
            message: '添加成功',
        });
    } else {
        res.json({
            code: 500,
            message: '添加失败',
        });
    }
}


// 修改
exports.change = async (req, res) => {
    let { changedata } = req.body
    let _id = changedata._id
    const data = await DoctorData.findById({ _id })

    console.log('data', data);


    for (const key in changedata) {
        if (changedata[key] && key != "_id") {
            console.log(123);
            console.log('changedata[key]', changedata[key], key);
            await DoctorData.findByIdAndUpdate({ _id }, { [key]: changedata[key] })
        }
    }
    res.json({
        code: 0,
        message: '修改成功',
    })

}
